package com.traffic.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Const;
import com.traffic.constant.Status;
import com.traffic.model.ApiResponse;
import com.traffic.service.BaidumapService;
import com.traffic.util.BaidumapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "百度地图相关接口")
@RestController
@RequestMapping(value = "/baidumap")
@Slf4j
public class BaidumapController {

    @Autowired
    BaidumapUtil baidumapUtil;

    @Autowired
    BaidumapService baidumapService;

    @ApiOperation(value = "【轨迹路况】", notes = "轨迹附近路况")
    @PostMapping("/road")
    @ApiJsonObject(name = "road",value = {
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "118.23456" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.34567" , description = "纬度"),
                })
    public ApiResponse getRoad(@RequestBody Map<String, Object> cor) throws UnsupportedEncodingException,NoSuchAlgorithmException {

        String ak = baidumapUtil.getAk();
//        Long loc_time = System.currentTimeMillis()/1000L;
        Double longitude = new Double(cor.get("longitude").toString());
        Double latitude = new Double(cor.get("latitude").toString());

        Map<String,Object> params = new HashMap<String,Object>();

        params.put("center", cor.get("latitude").toString()+","+cor.get("longitude").toString());
        params.put("location", cor.get("latitude").toString()+","+cor.get("longitude").toString());
        params.put("ak", ak);
        params.put("radius", 10);


        String roadname =baidumapService.getRoadname(params);
        params.put("road_name", roadname);

        String city = baidumapService.getCityname(params);
        params.put("city", city);

        String roadtraffic = baidumapService.getRoadtraffic(params);
        JSONObject json = JSON.parseObject(roadtraffic);

        return ApiResponse.ofStatus(Status.OK,json);

    }


    @ApiOperation(value = "【轨迹路况】", notes = "获取城市名字")
    @PostMapping("/city")
    @ApiJsonObject(name = "road",value = {
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "118.23456" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.34567" , description = "纬度"),
    })
    public ApiResponse getCity(@RequestBody Map<String, Object> cor) throws UnsupportedEncodingException,NoSuchAlgorithmException {

        String ak = baidumapUtil.getAk();
//        Long loc_time = System.currentTimeMillis()/1000L;
        Double longitude = new Double(cor.get("longitude").toString());
        Double latitude = new Double(cor.get("latitude").toString());

        Map<String,Object> params = new HashMap<String,Object>();

        params.put("center", cor.get("latitude").toString()+","+cor.get("longitude").toString());
        params.put("location", cor.get("latitude").toString()+","+cor.get("longitude").toString());
        params.put("ak", ak);
        params.put("radius", 10);


        String city = baidumapService.getCityname(params);
        return ApiResponse.ofStatus(Status.OK,city);
    }


    @ApiOperation(value = "【坐标解析地址】", notes = "坐标解析地址测试")
    @PostMapping("/address")
    @ApiJsonObject(name = "road",value = {
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "118.23456" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.34567" , description = "纬度"),
    })
    public ApiResponse getAddress(@RequestBody Map<String, Object> cor) throws UnsupportedEncodingException,NoSuchAlgorithmException {

        String ak = baidumapUtil.getAk();

        Double longitude = new Double(cor.get("longitude").toString());
        Double latitude = new Double(cor.get("latitude").toString());

        Map<String,Object> params = new HashMap<String,Object>();

        params.put("location", cor.get("latitude").toString()+","+cor.get("longitude").toString());
        params.put("ak", ak);


        String address = baidumapService.getAddress(params);
        JSONObject json = JSON.parseObject(address).getJSONObject("result");

        if(ObjectUtil.isEmpty(json)){return ApiResponse.ofStatus(Status.OK,"附近道路通行正常");}

  //      JSONArray pois = json.getJSONArray("pois");
  //      JSONObject poi = pois.getJSONObject(0);

  //      String detail = poi.getString("name");


        return ApiResponse.ofStatus(Status.OK,json);

    }


    @ApiOperation(value = "【地址解析坐标】", notes = "地址解析坐标测试")
    @PostMapping("/location")
    @ApiJsonObject(name = "location",value = {
            @ApiJsonProperty(type =String.class,key = "address",example = "北京市海淀区上地十街10号" , description = "地址")

    })
    public ApiResponse getLocation(@RequestBody Map<String, Object> add) throws UnsupportedEncodingException,NoSuchAlgorithmException {

        String ak = baidumapUtil.getAk();

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("address", add.get("address"));
        params.put("ak", ak);

        String address = baidumapService.getLocation(params);
        JSONObject json = JSON.parseObject(address).getJSONObject("result");
        JSONObject detail = json.getJSONObject("location");


        return ApiResponse.ofStatus(Status.OK,detail);

    }


}
